/*
 * 文 件    名:  AQModel.java
 * 创建日期:  2012-10-29上午10:57:43
 * 版          权:  Epro Technologies Co., Ltd. Copyright 2011-2012,  All rights reserved
 * 作          者:  tj072
 */
package com.weipu.common.facade.model;

import java.io.Serializable;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/** 
 * @Description  问题单model
 * @version [版本号, 2012-10-29]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 * 
 * @author tj072
 */
public class AQModel implements Serializable
{
    /** 
     * @Fields serialVersionUID : 版本序列号
     */
    private static final long serialVersionUID = -4343511016644922456L;
    
    /**
     * beforeQuestion问题
     */
    private String beforeQuestion;
    
    /**
     * beforeAnswer答案
     */
    private String beforeAnswer;
    
    /**
     * afterQuestion问题
     */
    private String afterQuestion;
    
    /**
     * afterAnswer答案
     */
    private String afterAnswer;
    
    public String getBeforeQuestion()
    {
        return beforeQuestion;
    }
    
    public void setBeforeQuestion(String beforeQuestion)
    {
        this.beforeQuestion = beforeQuestion;
    }
    
    public String getBeforeAnswer()
    {
        return beforeAnswer;
    }
    
    public void setBeforeAnswer(String beforeAnswer)
    {
        this.beforeAnswer = beforeAnswer;
    }
    
    public String getAfterQuestion()
    {
        return afterQuestion;
    }
    
    public void setAfterQuestion(String afterQuestion)
    {
        this.afterQuestion = afterQuestion;
    }
    
    public String getAfterAnswer()
    {
        return afterAnswer;
    }
    
    public void setAfterAnswer(String afterAnswer)
    {
        this.afterAnswer = afterAnswer;
    }
    
    @Override
    public String toString()
    {
        return tojson().toString();
    }
    
    public JSONObject tojson()
    {
        JSONObject json = new JSONObject();
        
        try
        {
            if (null != beforeQuestion)
            {
                json.put("beforeQuestion", beforeQuestion);
            }
            
            if (null != beforeAnswer)
            {
                json.put("beforeAnswer", beforeAnswer);
            }
            if (null != afterQuestion)
            {
                json.put("afterQuestion", afterQuestion);
            }
            
            if (null != afterAnswer)
            {
                json.put("afterAnswer", afterAnswer);
            }
            
        }
        catch (JSONException e)
        {
        }
        
        return json;
    }
    
    /**
     * JSON对象转存到问答model
     * @param json
     * @return  AQModel对象
     * @return AQModel
     * @exception throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
    */
    public static AQModel json2AQModel(JSONObject json)
    {
        AQModel model = new AQModel();
        if (null != json)
        {
            model.setBeforeQuestion(json.optString("beforeQuestion"));
            model.setBeforeAnswer(json.optString("beforeAnswer"));
            model.setAfterQuestion(json.optString("afterQuestion"));
            model.setAfterAnswer(json.optString("afterAnswer"));
        }
        
        return model;
    }
    
    /**
      * JSON字符串转存到问答model
      * @param jsonString
      * @return  AQModel对象
      * @return AQModel [返回类型说明]
      * @exception throws [违例类型] [违例说明]
      * @see [类、类#方法、类#成员]
     */
    public static AQModel json2AQModel(String jsonString)
    {
        if (null == jsonString || "".equals(jsonString.trim()))
        {
            return null;
        }
        
        AQModel model = null;
        try
        {
            JSONObject json = new JSONObject(jsonString);
            model = json2AQModel(json);
        }
        catch (JSONException e)
        {
        }
        return model;
    }
    
    /**  
      *  JSON数组转存到数组列表
      * @param arrays
      * @return  AQModel对象的数组
      * @return ArrayList<AQModel> 
      * @exception throws [违例类型] [违例说明]
      * @see [类、类#方法、类#成员]
     */
    public static ArrayList<AQModel> jsonArray2AQModel(JSONArray arrays)
    {
        ArrayList<AQModel> AQModelList = null;
        if (null != arrays && arrays.length() > 0)
        {
            AQModelList = new ArrayList<AQModel>(arrays.length());
            for (int i = 0; i < arrays.length(); i++)
            {
                AQModelList.add(AQModel.json2AQModel(arrays.optJSONObject(i)));
            }
        }
        return AQModelList;
    }
}
